import { defineStore } from "pinia";
import { getBookCategories, getBannerBooks, getNewBooks, getRecommendBooks } from "@/api/book";
export const useBookStore = defineStore("book", {
  state: () => ({
    BookCategory: [],

    // 轮播图
    bannerBook: [],
    // 新书推荐
    newBooks: [],
    // 推荐图书
    recommendBooks: [],
  }),
  actions: {
    async addBookCategeory() {
      const response = await getBookCategories()
      console.log("图书分类：", response.data.data)
      this.BookCategory = response.data.data
    },
    getBookCategeory(categoryId) {
      // 查找匹配的分类对象
      const category = this.BookCategory.find(
        item => item.categoryId === categoryId
      );

      if (category) {
        // console.log("图书分类名称：", category.categoryName);
        return category.categoryName; // 返回完整的分类对象
      } else {
        // console.log("未找到分类ID为", categoryId, "的分类");
        return null;
      }
    },
    async getBanner() {
      const response = await getBannerBooks()
      console.log("轮播图书：", response.data.data)
      this.bannerBook = response.data.data
    },
    async getNew() {
      const response = await getNewBooks()
      console.log("新书推荐：", response.data.data)
      this.newBooks = response.data.data
    },
    async getRecommend() {
      const response = await getRecommendBooks()
      console.log("推荐图书：", response.data.data)
      this.recommendBooks = response.data.data
    }

  }
});